package com.letv.android.client.album.controller;

import com.ledim.app.LeDimApplication;
import com.ledim.bean.ExpressionPackageBean;
import com.ledim.bean.base.BaseListResponse;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.LogUtil;
import java.util.HashMap;
import java.util.List;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ccx on 16/7/12.
 * 表情包下载控制类
 */
public class AlbumRoomEmojController {

    private List<ExpressionPackageBean> emojListDatas;//emoj表情包列表
    private Subscription getEmjListSubscription;//获取表情包列表
    private Subscription getSinggleEmjDatasSubscription;
    private HashMap<String, List<ExpressionPackageBean>> emjDatasMap;

    public AlbumRoomEmojController() {
        emjDatasMap = new HashMap<String, List<ExpressionPackageBean>>();
    }

    /**
     * 获取表情包列表
     */
    public void getEmojListData(final EmjDataObserve emjDataObserve) {
        if (emojListDatas == null) {
            if (getEmjListSubscription != null) {
                getEmjListSubscription.unsubscribe();
            }
            getEmjListSubscription = LedimApiServiceFactory.createService(ApiService.class)
                                                           .getExpressionPackage(LeDimApplication.getInstance()
                                                                                                .getAppVersionName())
                                                           .observeOn(AndroidSchedulers.mainThread())
                                                           .subscribeOn(Schedulers.io())
                                                           .subscribe(new LedimHttpObserver<BaseListResponse<ExpressionPackageBean>>() {
                                                              @Override
                                                              public void onCompleted() {
                                                                  super.onCompleted();
                                                              }

                                                              @Override
                                                              public void onError(Throwable e) {
                                                                  super.onError(e);
                                                                  LogUtil.e(e.getMessage());
                                                              }

                                                              @Override
                                                              public void onNext(
                                                                      BaseListResponse<ExpressionPackageBean> datas) {
                                                                  super.onNext(datas);
                                                                  if (datas != null &&
                                                                          datas.success) {
                                                                      emojListDatas = datas.data;
                                                                      emjDataObserve.onGetEmjList(emojListDatas);
                                                                  }

                                                              }
                                                          });
        } else {
            emjDataObserve.onGetEmjList(emojListDatas);
        }
    }

    /**
     * 通过id查找表情包数据
     */
    public void getEmojData(final String id, final EmjDataObserve emjDataObserve) {
        if (emjDatasMap.containsKey(id)) {
            emjDataObserve.onGetEmjData(emjDatasMap.get(id));
        } else {
            getSinggleEmjDatasSubscription = LedimApiServiceFactory.createService(ApiService.class)
                                                                   .getExpressionFromPackageId(id)
                                                                   .observeOn(AndroidSchedulers.mainThread())
                                                                   .subscribeOn(Schedulers.io())
                                                                   .subscribe(new LedimHttpObserver<BaseListResponse<ExpressionPackageBean>>() {

                                                                      @Override
                                                                      public void onCompleted() {
                                                                          super.onCompleted();
                                                                      }

                                                                      @Override
                                                                      public void onError(
                                                                              Throwable e) {
                                                                          super.onError(e);
                                                                          LogUtil.e(e.getMessage());
                                                                      }

                                                                      @Override
                                                                      public void onNext(
                                                                              BaseListResponse<ExpressionPackageBean> datas) {
                                                                          super.onNext(datas);
                                                                          if (datas != null) {
                                                                              emjDatasMap.put(id, datas.data);
                                                                              emjDataObserve.onGetEmjData(datas.data);
                                                                          } else {
                                                                              ////  ToastUtil.toast
                                                                              //          (datas.message);
                                                                          }
                                                                      }
                                                                  });
        }

    }

    public void onDestroy() {
        if (getEmjListSubscription != null) {
            getEmjListSubscription.unsubscribe();
        }
        if (getSinggleEmjDatasSubscription != null) {
            getSinggleEmjDatasSubscription.unsubscribe();
        }
    }

    public interface EmjDataObserve {

        void onGetEmjList(List<ExpressionPackageBean> datas);

        void onGetEmjData(List<ExpressionPackageBean> emojDatas);
    }
}
